<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px" @keyup.enter.native="handleQuery">
                <el-form-item label="handle" prop="expert_name">
                    <el-input v-model.trim="queryParams.expert_name" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="合创码" prop="creation_code">
                    <el-input v-model.trim="queryParams.creation_code" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="负责人" prop="fb_admin_id">
                    <SelectPage
                        v-model="queryParams.fb_admin_id"
                        placeholder="请选择"
                        :url="$api.purchase.getPurchaserList"
                        show_field="username"
                        show_id="operater_id"
                        query_field="keyword"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="sku" prop="sku">
                    <el-input v-model.trim="queryParams.sku" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="发布时间" prop="publish_time">
                    <el-date-picker
                        v-model="queryParams.publish_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>
            <!-- <el-tabs v-model="queryParams.status" type="border-card" @tab-click="handleQuery">
                <el-tab-pane v-for="item in tabsList" :key="item.value" :label="item.label" :name="item.value.toString()"></el-tab-pane>
            </el-tabs>
            <div class="table-wrap"> -->
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column v-if="showColumn('expert_id')" label="达人ID" prop="expert_id" min-width="140"></el-table-column>
                <el-table-column v-if="showColumn('expert_name')" label="handle" prop="expert_name" min-width="160">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="gotoPersonalDetail(scope.row)">{{ scope.row.expert_name }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('creation_code')" label="合创码" prop="creation_code" min-width="220">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.creation_code" placement="top">
                            <span class="ellipsis-2">{{ scope.row.creation_code }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('video_url')" label="视频地址" prop="video_url" min-width="220">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.video_url" placement="top">
                            <el-link v-if="scope.row.video_url" type="primary" :href="scope.row.video_url" target="_blank" class="ellipsis-2">{{ scope.row.video_url }}</el-link>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('sku')" label="SKU" prop="sku" min-width="160">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.sku" placement="top">
                            <span class="ellipsis-2">{{ scope.row.sku }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('operater')" label="负责人" prop="operater" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('fb_admin_names')" label="投放" prop="fb_admin_names" min-width="120"></el-table-column>
                <el-table-column v-if="showColumn('remarks')" label="备注" prop="remarks" min-width="160">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.remarks" placement="top">
                            <span class="ellipsis-2">{{ scope.row.remarks }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('remarks_exception')" label="问题反馈" prop="remarks_exception" min-width="160">
                    <template slot-scope="scope">
                        <el-tooltip :content="scope.row.remarks_exception" placement="top">
                            <span class="ellipsis-2">{{ scope.row.remarks_exception }}</span>
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column v-if="showColumn('publish_time')" label="发布日期" prop="publish_time" min-width="160"></el-table-column>
                <el-table-column v-if="showColumn('create_time')" label="创建时间" prop="create_time" min-width="160"></el-table-column>
                <el-table-column label="操作" min-width="120" fixed="right">
                    <template slot-scope="scope">
                        <div>
                            <el-button type="text" icon="el-icon-edit" style="padding: 4px 0" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                        </div>
                        <div>
                            <el-button type="text" icon="el-icon-delete" style="padding: 4px 0" @click="deleteRow(scope.row)">删除</el-button>
                        </div>

                        <!-- <el-button v-if="scope.row.status == 1" type="text" icon="el-icon-tickets" @click="showAuditDialog(scope.row)">审核</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <!-- </div> -->
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>

        <el-dialog :title="title" :visible.sync="auditDialog.visible" width="420px" append-to-body>
            <el-form ref="form" :model="auditDialog.data" :rules="rules" inline label-width="100px" label-suffix=":">
                <el-form-item label="审核结果" prop="status">
                    <el-select v-model="auditDialog.data.status" placeholder="请选择" clearable size="small">
                        <el-option v-for="(dict, index) in approvalStatus" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" :loading="saveLoading" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getVideoList, checkExpert, delVideo } from '@/api/expert'
import { APPROVAL_STATUS, TABS_LIST } from '../constant'
import DetailDialog from './components/DetailDialog.vue'
export default {
    name: 'ShopList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 负责人
            salesmanList: [],
            // 审批结果
            approvalStatus: APPROVAL_STATUS,
            tabsList: TABS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                expert_name: undefined,
                creation_code: undefined,
                fb_admin_id: undefined,
                sku: undefined,
                publish_time: [],
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            saveLoading: false,
            auditDialog: {
                visible: false,
                data: {
                    id: undefined,
                    status: undefined,
                },
            },
            rules: {},
            title: '审核',
            columns: [
                { label: '达人ID', prop: 'expert_id', visible: true },
                { label: 'handle', prop: 'expert_name', visible: true },
                { label: '合创码', prop: 'creation_code', visible: true },
                { label: '视频地址', prop: 'link', visible: true },
                { label: 'SKU', prop: 'sku', visible: true },
                { label: '负责人', prop: 'operater', visible: true },
                { label: '投放', prop: 'fb_admin_names', visible: true },
                { label: '备注', prop: 'remarks', visible: true },
                { label: '问题反馈', prop: 'remarks_exception', visible: true },
                { label: '发布日期', prop: 'publish_time', visible: true },
                { label: '创建时间', prop: 'create_time', visible: true },
            ],
        }
    },
    created() {
        this.getList()
    },
    methods: {
        gotoPersonalDetail({expert_id}) {
            this.$router.push({
                path: '/kol/expertView',
                query: {
                    expert_id,
                },
            })
        },
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        deleteRow(row) {
            this.$confirm('是否确认删除该数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delVideo({ id: row.id }).then(res => {
                        this.$message.success('删除成功')
                        this.handleQuery()
                    })
                })
                .catch(() => {})
        },
        submit() {
            this.saveLoading = true
            checkExpert(this.auditDialog.data)
                .then(() => {
                    this.auditDialog.visible = false
                    this.handleQuery()
                    this.$message.success('操作成功')
                })
                .finally(() => {
                    this.saveLoading = false
                })
        },
        cancel() {
            this.auditDialog.visible = false
        },
        showAuditDialog(row) {
            this.auditDialog.visible = true
            this.auditDialog.data.id = row.id
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            getVideoList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style>
.ellipsis-2 .el-link--inner {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
</style>
<style scoped>
.ellipsis-2 {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
